<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>=^.^=</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div class="info">
	<p><a href="https://github.com/yomotsu/camera-controls">GitHub repo</a></p>
	<button type="button" onclick="cameraControls.normalizeRotations().reset( true )">reset</button>
	<ul>
		<li>drag w/o keys: rotate</li>
		<li>drag w/ <kbd>shift key</kbd>: translate</li>
		<li>drag w/ <kbd>control key</kbd>: dolly</li>
	</ul>
</div>

<script async src="https://unpkg.com/es-module-shims@2.6.1/dist/es-module-shims.js"></script>
<script type="importmap">
{
	"imports": {
		"three": "https://unpkg.com/three@0.178.0/build/three.module.js",
		"three/addons/": "https://unpkg.com/three@0.178.0/examples/jsm/"
	}
}
</script>
<script type="module">
import * as THREE from 'three';
import CameraControls from './dist/camera-controls.module.js';
CameraControls.install( { THREE: THREE } );

const width = window.innerWidth;
const height = window.innerHeight;
const clock = new THREE.Clock();
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 60, width / height, 0.01, 100 );
camera.position.set( 0, 0, 5 );
const renderer = new THREE.WebGLRenderer();
renderer.setSize( width, height );
document.body.appendChild( renderer.domElement );

const cameraControls = new CameraControls( camera, renderer.domElement );

// switch the behavior by the modifier key press
const keyState = {
	shiftRight  : false,
	shiftLeft   : false,
	controlRight: false,
	controlLeft : false,
};

const updateConfig = () => {

	if ( keyState.shiftRight || keyState.shiftLeft ) {

		cameraControls.mouseButtons.left = CameraControls.ACTION.TRUCK;

	} else if ( keyState.controlRight || keyState.controlLeft ) {

		cameraControls.mouseButtons.left = CameraControls.ACTION.DOLLY;

	} else {

		cameraControls.mouseButtons.left = CameraControls.ACTION.ROTATE;

	}

}

document.addEventListener( 'keydown', ( event ) => {

	if ( event.code === 'ShiftRight'   ) keyState.shiftRight   = true;
	if ( event.code === 'ShiftLeft'    ) keyState.shiftLeft    = true;
	if ( event.code === 'ControlRight' ) keyState.controlRight = true;
	if ( event.code === 'ControlLeft'  ) keyState.controlLeft  = true;
	updateConfig();

} );

document.addEventListener( 'keyup', ( event ) => {

	if ( event.code === 'ShiftRight'   ) keyState.shiftRight   = false;
	if ( event.code === 'ShiftLeft'    ) keyState.shiftLeft    = false;
	if ( event.code === 'ControlRight' ) keyState.controlRight = false;
	if ( event.code === 'ControlLeft'  ) keyState.controlLeft  = false;
	updateConfig();

} );

const mesh = new THREE.Mesh(
	new THREE.BoxGeometry( 1, 1, 1 ),
	new THREE.MeshBasicMaterial( { color: 0xff0000, wireframe: true } )
);
scene.add( mesh );

const gridHelper = new THREE.GridHelper( 50, 50 );
gridHelper.position.y = - 1;
scene.add( gridHelper );

renderer.render( scene, camera );

( function anim () {

	const delta = clock.getDelta();
	const updated = cameraControls.update( delta );

	requestAnimationFrame( anim );

	if ( updated ) {

		renderer.render( scene, camera );
		console.log( 'rendered' );

	}

} )();

// make variable available to browser console
globalThis.cameraControls = cameraControls;
</script>

</body>
</html>
